# Function for Aggregating Behavioral Proclivities 

get_dummy_ml <- function(new_var, old_vars, pos_level, neg_level) {
  for(i in 1:length(old_vars)) {
    # uses <<- to perform assignment in global environment:
    ml[,new_var][ml[,old_vars[i]] == pos_level] <<- 1
    ml[,new_var][ml[,old_vars[i]] == neg_level] <<- 0
  }
  return(data)
}


# Conative Attitudes ------------------------------------------------------

# Absenteeism -------------------------------------------------------------

abs_vars <- c("lc1",
              "pta",
              "bring_up",
              "assemble")

ml[,abs_vars] <- NA

mlcb$fire_teach_q10_1_1$answer[2,]
mlcb$fire_teach_q10_1_2$answer[2,]
mlcb$fire_teach_q10_1_3$answer[2,]
mlcb$fire_teach_q10_1_4$answer[2,]
mlcb$pta_q10_2_1$answer[1,]
mlcb$pta_q10_2_2$answer[1,]
mlcb$pta_q10_2_3$answer[1,]
mlcb$pta_q10_2_4$answer[1,]
mlcb$bring_up_q10_3_1$answer[2,]
mlcb$bring_up_q10_3_2$answer[2,]
mlcb$bring_up_q10_3_3$answer[2,]
mlcb$bring_up_q10_3_4$answer[2,]
mlcb$assemble_q10_4_1$answer[1,]
mlcb$assemble_q10_4_2$answer[1,]
mlcb$assemble_q10_4_3$answer[1,]
mlcb$assemble_q10_4_4$answer[1,]

mapply(FUN = get_dummy_ml,
  new_var = abs_vars,
  old_vars = list(c("fire_teach_q10_1_1",
                    "fire_teach_q10_1_2",
                    "fire_teach_q10_1_3",
                    "fire_teach_q10_1_4"),
                  c("pta_q10_2_1",
                    "pta_q10_2_2",
                    "pta_q10_2_3",
                    "pta_q10_2_4"),
                  c("bring_up_q10_3_1",
                    "bring_up_q10_3_2",
                    "bring_up_q10_3_3",
                    "bring_up_q10_3_4"),
                  c("assemble_q10_4_1",
                    "assemble_q10_4_2",
                    "assemble_q10_4_3",
                    "assemble_q10_4_4")),
  pos_level = c(2,1,2,1),
  neg_level = c(1,2,1,2))

rm(abs_vars)

# Additional variables for repsondents who are LC1s

lc1s <- c("fire_teach_q10_1_1_2",
          "fire_teach_q10_1_2_2",
          "fire_teach_q10_1_3_2",
          "fire_teach_q10_1_4_2")

mlcb$fire_teach_q10_1_1_2$answer[2,]
mlcb$fire_teach_q10_1_2_2$answer[2,]
mlcb$fire_teach_q10_1_3_2$answer[2,]
mlcb$fire_teach_q10_1_4_2$answer[2,]

for(variable in lc1s) {
  ml$lc1[ml[,variable] == 2] <- 1
  ml$lc1[ml[,variable] == 1] <- 0
}

rm(lc1s)

# IPV ---------------------------------------------------------------------

# The action in line with reporting is 1 for the first q,
# 2 for the second, 1 for the third, and 2 for the fourth
mlcb$action_q8_1a_1$answer[1, ]
mlcb$action_q8_1a_2$answer[1, ]
mlcb$action_q8_1a_3$answer[1, ]
mlcb$action_q8_1a_4$answer[1, ]
mlcb$action_q8_1b_1$answer[2, ]
mlcb$action_q8_1b_2$answer[2, ]
mlcb$action_q8_1b_3$answer[2, ]
mlcb$action_q8_1b_4$answer[2, ]
mlcb$action_q8_1c_1$answer[1, ]
mlcb$action_q8_1c_2$answer[1, ]
mlcb$action_q8_1c_3$answer[1, ]
mlcb$action_q8_1c_4$answer[1, ]
mlcb$action_q8_1d_1$answer[2, ]
mlcb$action_q8_1d_2$answer[2, ]
mlcb$action_q8_1d_3$answer[2, ]
mlcb$action_q8_1d_4$answer[2, ]

# Create blank variables
ipv_vars <- c("report_police",
              "involve_lc1",
              "involve_parents",
              "involve_nabakyala")
ml[, ipv_vars] <- NA

# mapply function through parameters
mapply(
  FUN = get_dummy_ml,
  new_var = ipv_vars,
  old_vars = list(
    c(
      "action_q8_1a_1",
      "action_q8_1a_2",
      "action_q8_1a_3",
      "action_q8_1a_4"
    ),
    c(
      "action_q8_1b_1",
      "action_q8_1b_2",
      "action_q8_1b_3",
      "action_q8_1b_4"
    ),
    c(
      "action_q8_1c_1",
      "action_q8_1c_2",
      "action_q8_1c_3",
      "action_q8_1c_4"
    ),
    c(
      "action_q8_1d_1",
      "action_q8_1d_2",
      "action_q8_1d_3",
      "action_q8_1d_4"
    )
  ),
  pos_level = c(1, 2, 1, 2),
  neg_level = c(2, 1, 2, 1)
)

rm(ipv_vars)

# Additional variables for repsondents who are lc1s
lc1s <- c("action_q8_1b_2_1",
          "action_q8_1b_2_2",
          "action_q8_1b_2_3",
          "action_q8_1b_2_4")

# Answer 2 is in line with reporting for LC1s
mlcb$action_q8_1b_2_2$answer[2, ]
mlcb$action_q8_1b_2_2$answer[2, ]
mlcb$action_q8_1b_2_3$answer[2, ]
mlcb$action_q8_1b_2_4$answer[2, ]

for (variable in lc1s) {
  ml$involve_lc1[ml[, variable] == 2] <- 1
  ml$involve_lc1[ml[, variable] == 1] <- 0
}

rm(lc1s)

# Additional variables for repsondents who are nabakyalas
nabas <- c("action_q8_1d_2_1",
           "action_q8_1d_2_2",
           "action_q8_1d_2_3",
           "action_q8_1d_2_4")

# Answer 2 is in line with reporting for Nabakyalas
mlcb$action_q8_1d_2_1$answer[2, ]
mlcb$action_q8_1d_2_2$answer[2, ]
mlcb$action_q8_1d_2_3$answer[2, ]
mlcb$action_q8_1d_2_4$answer[2, ]

for (variable in nabas) {
  ml$involve_nabakyala[ml[, variable] == 2] <- 1
  ml$involve_nabakyala[ml[, variable] == 1] <- 0
}

rm(nabas)

# Abortion ----------------------------------------------------------------


abo_vars <- c("vht",
              "see_doctor",
              "advise_councelling",
              "compassion")

ml[,abo_vars] <- NA

mlcb$niece_q12_4a_1$answer[2,]
mlcb$niece_q12_4a_2$answer[2,]
mlcb$niece_q12_4b_1$answer[1,]
mlcb$niece_q12_4b_2$answer[1,]
mlcb$eight_children_q12_5a_1$answer[2,]
mlcb$eight_children_q12_5a_2$answer[2,]
mlcb$eight_children_q12_5b_1$answer[1,]
mlcb$eight_children_q12_5b_2$answer[1,]

mapply(
  FUN = get_dummy_ml,
  new_var = abo_vars,
  old_vars = list(c("niece_q12_4a_1",
                    "niece_q12_4a_2"),
                  c("niece_q12_4b_1",
                    "niece_q12_4b_2"),
                  c("eight_children_q12_5a_1",
                    "eight_children_q12_5a_2"),
                  c("eight_children_q12_5b_1",
                    "eight_children_q12_5b_2")),
  pos_level = c(2,1,2,1),
  neg_level = c(1,2,1,2))

rm(abo_vars)

# Additional variables for repsondents who are VHTs

mlcb$niece_q12_4a_1_2$answer[2,]
mlcb$niece_q12_4a_2_2$answer[2,]

vhts <- c("niece_q12_4a_1_2",
          "niece_q12_4a_2_2")

for(variable in vhts) {
  ml$vht[ml[,variable] == 2] <- 1
  ml$vht[ml[,variable] == 1] <- 0
}

rm(vhts)

# Additional variables for repsondents who are nabakyalas

mlcb$eight_children_q12_5a_1_2$answer[2,]
mlcb$eight_children_q12_5a_2_2$answer[2,]

nabas <- c("eight_children_q12_5a_1_2",
           "eight_children_q12_5a_2_2")

for(i in 1:length(nabas)) {
  ml$advise_councelling[ml[,variable] == 2] <- 1
  ml$advise_councelling[ml[,variable] == 1] <- 0
}

rm(nabas)



rm(i,variable,get_dummy_ml)

